<?php

class MessagesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $this->_redirect('authentication/inbox');
    }

    public function inboxAction()
    {
    	$auth = Zend_Auth::getInstance();
       	if ($auth->hasIdentity()) 
        {
            $user_id = $auth->getIdentity()->id; 
        }
        else 
        {
        	$this->_redirect('authentication/login');
        }
        
    	$inboxMessageList = new Application_Model_MessagesTbl();
    	$inboxMessageList = $inboxMessageList->getInboxList($this->view->playerStats['alias']);
    	
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($inboxMessageList));
        $paginator->setItemCountPerPage(15)
        		  ->setCurrentPageNumber($this->_getParam('page',1));
        $this->view->paginator = $paginator;

        $page = $paginator->getCurrentPageNumber();
        $gotoPageForm = new Application_Form_Pagination();
       	$gotoPageForm->pageNumber->setValue($page);
       	$this->view->gotoPage = $gotoPageForm;

    	if($this->getRequest()->isPost())
	    {
	    	if($gotoPageForm->isValid($_POST))
	    	{
	    		$gotoPageNumber = $gotoPageForm->getValue('pageNumber');
	    		if($gotoPageNumber > $paginator->count() )
	    		{
	    			$gotoPageNumber = $paginator->count();
	    		}
    			$paginator->setCurrentPageNumber($this->_getParam('page',$gotoPageNumber));
    			$redirector = $this->_helper->getHelper('Redirector');
	    		$redirector->gotoSimple('inbox',
                                       'messages',
                                       null,
                                       array('page' => $gotoPageNumber
                                             ));		
	    	}

	    }
        
    }

    public function sentAction()
    {
    	$sentMessageList = new Application_Model_MessagesTbl();
    	$sentMessageList = $sentMessageList->getSentList($this->view->playerStats['alias']);
    	
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($sentMessageList));
        $paginator->setItemCountPerPage(15)
        		  ->setCurrentPageNumber($this->_getParam('page',1));
        $this->view->paginator = $paginator;

        $page = $paginator->getCurrentPageNumber();
        $gotoPageForm = new Application_Form_Pagination();
       	$gotoPageForm->pageNumber->setValue($page);
       	$this->view->gotoPage = $gotoPageForm;

    	if($this->getRequest()->isPost())
	    {
	    	if($gotoPageForm->isValid($_POST))
	    	{
	    		$gotoPageNumber = $gotoPageForm->getValue('pageNumber');
	    		if( $gotoPageNumber > $paginator->count() )
	    		{
	    			$gotoPageNumber = $paginator->count();
	    		}
    			$paginator->setCurrentPageNumber($this->_getParam('page',$gotoPageNumber));
    			$redirector = $this->_helper->getHelper('Redirector');
	    		$redirector->gotoSimple('sent',
                                       'messages',
                                       null,
                                       array('page' => $gotoPageNumber
                                             ));		
	    	}

	    }
        
    }

    public function writeAction()
    {
        $message = new Application_Form_Message();
        $this->view->message = $message;
		$roundSettingsTbl = new Application_Model_RoundSettingsTbl();
        $messageTbl = new Application_Model_MessagesTbl();
        
    	if($this->getRequest()->isPost()){
    		if($message->isValid($_POST)){
    			$message->fromAlias->setValue($this->view->playerStats['alias']);
    			$data = $message->getValues();
    			if( !$roundSettingsTbl->checkIfAliasExists($data['toAlias']) ){
    				$errorMessage = $errorMessage."<br/>Invalid player alias!";
    				$this->view->errorMessage = $errorMessage;
    				return;
    			}
    			$messageTbl->insert($data);
    			$this->_redirect('messages/inbox');
    		}
    	}
    }

    public function readAction()
    {
    	if( $_GET['m'] != null){
    		$messageID = $_GET['m'];
    	}
    	else {
    		$this->_redirect('messages/inbox');
    	}
    	
    	
    	$readMessage = new Application_Model_MessagesTbl();
    	$readMessage  = $readMessage->readMessage($messageID);
    	
    	$this->view->readMessage = $readMessage;
    }


}









